<?php
if(!defined('CMS'))exit;
if (isset($_POST, $_POST['event'])) {
	global$Eleanor;
	$Eleanor->module['config'] = include($Eleanor->module['path'] .'config.php');
	Eleanor::LoadOptions($Eleanor->module['config']['opts']);

	$d = (string)$_POST['event'];

	switch($d) {
		case'select_model':
            require_once $Eleanor->module['path'] .'service.php';
            $api = new QuestionServices();
            if (ctype_digit($_POST['id'])) {
                $l = Eleanor::$Language->Load($Eleanor->module['path'] .'lang_user-*.php', $Eleanor->module['config']['n']);
                $marki = '';
                $service = new QuestionServices();
                foreach ($api->CheckChild($_POST['id']) as $v) {
                    $marki .= '<li data-id="'. $v['id'] .'">'. $v['title'] .'</li>';
                }
                Result('<label>'. $l['choice_model'].'</label><div class="select_box">
            <input class="select_box_value" type="text" value="'. $l['choice_marka_m'].'" name="modeli_select"
            readonly="readonly">
            <input class="select_value" type="hidden" id="modeli_select" value="" name="modeli_select">
            <div class="show_down_triangle"></div>
            <div class="drop_down">
                <ul class="drop_down_list">
                    '. $marki .'
                </ul>
                <div class="scrollbar">
                    <div class="scrollable"></div>
                </div>
            </div>
        </div>');
            } else {
                Result(false);
            }
		break;
		case'save':
            $data = $_POST;
            $TC = new TimeCheck($Eleanor->module['id']);
            $ch = $TC->Check('add_q', false);
            if($ch) {
                $lang = Eleanor::$Language->Load($Eleanor->module['path'].'lang_user-*.php',$Eleanor->module['config']['n']);
                Error($lang['flood_limit']($ch['_datets'] - time()));
                return false;
            }

            $maincat = isset($data['modeli']) ? (int)$data['modeli'] : false;
            $cats = isset($data['marki']) ? array($data['marki'], $maincat) : array();
            if($cats && count($cats) > 1) {
                sort($cats, SORT_NUMERIC);
                if($maincat != $cats[0])
                    array_unshift($cats, $maincat ? $maincat : reset($cats));
            }
            $cats = $cats ? ','.join(',,',$cats).',' : '';

            $url = htmlspecialchars_decode($data['question'], ENT_QUOTES);
            $url = $Eleanor->Url->Filter($url);
            $values = array(
                'cats' => $cats,
                'date' => date('Y-m-d H:i:s'),
                'show_detail' => date('Y-m-d H:i:s'),
                'status' => 1,
                'email' => isset($data['email']) ? (string)$data['email'] : '',
                'author' => $data['author_name'] !== 'Anonim' ?  $data['author_name'] : 'Anonim',
                'author_id' => $data['author_name'] !== 'Anonim' ? Eleanor::getIdByLogin($data['author_name']) : 0,
            );

            $values_l = array(
                'id' => Eleanor::$Db->Insert($Eleanor->module['config']['t'], $values, null),
                'language' => '',
                'lstatus' => 1,
                'uri' => $url,
                'ldate' => date('Y-m-d H:i:s'),
                'lcats' => $cats,
                'title' => $data['question'],
                'text' => $data['text'],
            );
            Eleanor::$Db->Insert($Eleanor->module['config']['tl'], $values_l, null);
            if($flood=Eleanor::$Permissions->FloodLimit()) {
                $TC->Add('add_q','',true,$flood);
            }
            Result(true);
		break;
		case'comments':
            $id=isset($_POST['id']) ? (int)$_POST['id'] : 0;
            $R = Eleanor::$Db->Query('SELECT `id`,`cats`,`uri` FROM `'.$Eleanor->module['config']['t'].'` INNER JOIN `'.$Eleanor->module['config']['tl'].'` USING(`id`) WHERE `id`='.$id.' AND `language`IN(\'\',\''.Language::$main.'\') AND `status`=1 LIMIT 1');
            if(!$a=$R->fetch_assoc())
            return Error();
            BeAs('user');

            $Eleanor->Categories->Init($Eleanor->module['config']['c']);
            $cat=$a['cats'] && $Eleanor->Url->furl ? $Eleanor->Categories->GetUri((int)ltrim($a['cats'],',')) : false;
            $u=array('u'=>array($a['uri'],'nid'=>$a['id']));
            $data=isset($_POST['comments']) ? (array)$_POST['comments'] : array();
            $Eleanor->Comments_ajax->baseurl=array('module'=>$Eleanor->module['name'])+($cat ? $cat+$u : $u);
            if($r=$Eleanor->Comments_ajax->Process($data,$id))
            switch($r['event']) {
                case'post':
                if($Eleanor->Comments->rights['post']==1 and !$r['merged'])
                Eleanor::$Db->Update($Eleanor->module['config']['t'],array('!comments'=>'`comments`+1'),'`id`='.$id.' LIMIT 1');
                case'save':
                Eleanor::$Db->Update($Eleanor->module['config']['tl'],array('!last_mod'=>'NOW()'),'`id`='.$id.' LIMIT 1');
                break;
                case'delete':
                if($r['deleted']) {
                    Eleanor::$Db->Update($Eleanor->module['config']['t'],array('!comments'=>'IF(`comments`>'.$r['deleted'].',`comments`-'.$r['deleted'].',0)'),'`id`='.$id.' LIMIT 1');
                    Eleanor::$Db->Update($Eleanor->module['config']['tl'],array('!last_mod'=>'NOW()'),'`id`='.$id.' LIMIT 1');
                }
                break;
                case'moderate':
                if($r['activated']) {
                    Eleanor::$Db->Update($Eleanor->module['config']['t'],array('!comments'=>'GREATEST(0,`comments`'.($r['activated']>0 ? '+'.$r['activated'] : $r['activated']).')'),'`id`='.$id.' LIMIT 1');
                    Eleanor::$Db->Update($Eleanor->module['config']['tl'],array('!last_mod'=>'NOW()'),'`id`='.$id.' LIMIT 1');
                }
            }
		break;
	}
}